package com.example.androidcompletesec.fragment;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

import com.example.androidcompletesec.R;

public class QuestionDialogFragment extends DialogFragment { 
	NoticeDialogListener mListener; 
	Bundle bundle; 

	public void onAttach(Activity activity) { 
		super.onAttach(activity); 
		try{ 
			mListener = (NoticeDialogListener) activity; 
		}catch (ClassCastException e) { String msg = " must implement NoticeDialogListener"; 
			throw new ClassCastException(activity.toString() + msg);
		} 
	} 
	public Dialog onCreateDialog(Bundle savedInstanceState) { 
		bundle = getArguments(); 
		return new AlertDialog.Builder(getActivity()) 
		.setIcon(R.drawable.ic_launcher) 
		.setTitle(bundle.getString("title")) 
		.setMessage(bundle.getString("msg")) 
		.setPositiveButton(bundle.getString("Q1"), (android.content.DialogInterface.OnClickListener) listener)
		.setNegativeButton(bundle.getString("Q2"), (android.content.DialogInterface.OnClickListener) listener) 
		.create(); 
	}
	
	OnClickListener listener = new OnClickListener(){
		public void onClick(DialogInterface dialog, int which) {
			int a = bundle.getInt("a");
			int b = bundle.getInt("b");
			switch(which){
			case DialogInterface.BUTTON_POSITIVE:
				mListener.onDialogClick(QuestionDialogFragment.this, a+b);
				break;
			case DialogInterface.BUTTON_NEGATIVE:
				mListener.onDialogClick(QuestionDialogFragment.this,a*b);
				break;
			}
		}

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			
		}
	};
	public interface NoticeDialogListener {
		public void onDialogClick(DialogFragment dialog, int res);
	}
}
